﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using Joker;
using System.Net.Http.Headers;
using System.Drawing.Imaging;
using System.Data.Common;
using System.Diagnostics;

namespace J000
{
    public partial class MainPage : Form
    {
        public MainPage()
        {
            InitializeComponent();
        }
        //DataSet ComInfoSet = new DataSet();
        private void MainPage_Load(object sender, EventArgs e)
        {
            //DataSet ComInfoSet = Z.QuerySql();
            textBox1.Text = Z.aa(textBox1.Text);
            Z.ShowSqlText(CompLeteName, Sex, Address, CEOName, Bank, BankID, SociologyID);
            ImageName.DataSource = Z.QuerySql().Tables[1];
            ImageName.DisplayMember = Z.QuerySql().Tables[1].Columns[1].ToString();
            ImageName.ValueMember = Z.QuerySql().Tables[1].Columns[0].ToString();
            //ImageFile.Image = Z.ShowSqlImage(ImageName, CompanyImage.Name);
        }
        private void CL_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (JokerEntities en = new JokerEntities())
            {
                //通过下面这个命令可以看到执行的sql和详细过程
                en.Database.Log = s => Debug.WriteLine(s);
                Personnel p = new Personnel();
                //插入一个员工，N=最大N+1
                short new_N = Convert.ToInt16(en.Personnels.Max(x => x.N) + 1);
                p.N = new_N;
                p.PName = $"测试姓名_{new_N}";
                en.Personnels.Add(p);
                en.SaveChanges();
                MessageBox.Show("添加成功！");

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (JokerEntities en = new JokerEntities())
            {
                Personnel p = en.Personnels.ToList().Last();
                MessageBox.Show($"最后一个添加的员工信息\n{p.N}\t{p.PName}");
            }
        }

        private void BrowseLocal_Click(object sender, EventArgs e)
        {
            String FilePath = Z.OpenloadFile("选择照片", "图片文件(*.jpg,*.gif,*.bmp)|*.jpg;*.gif;*.bmp|AllFiles(*.*)|*.*");
            if (FilePath != null)
            {
                ImageFile.Load(FilePath);
            }
            else
            {
                ImageFile.Image = null;
            }
        }

        private void CV3_Click(object sender, EventArgs e)
        {

        }

        private void Dct_SelectedIndexChanged(object sender, EventArgs e)
        {
            //ImageFile.Image = Z.ShowSqlImage(ImageName, CompanyImage.Name);
        }

        private void EA_Click(object sender, EventArgs e)
        {
            GiveUpAll.Visible = true;
            Z.EditTextBoxNatrue(CompLeteName, Sex, Address, CEOName, Bank, BankID, SociologyID, false);
        }

        private void GiveUp_Click(object sender, EventArgs e)
        {
            //GiveUpAll.Visible = false;
            //Z.EditTextBoxNatrue(CompLeteName, Sex, Address, CEOName, Bank, BankID, SociologyID, true);
            //Z.EditButtonNatrue(EditCompleteName, EditSex, EditAddress, EditCEOName, EditBank, EditBankID, EditSociologyID, "修改");
            //Z.GiveUpButtonEdit(GiveUpCompleteName, GiveUpSex, GiveUpAddress, GiveUpCEOName, GiveUpBank, GiveUpBankID, GiveUpSociologyID, false);
            //Z.ShowSqlText(CompLeteName, Sex, Address, CEOName, Bank, BankID, SociologyID);
        }

        private void EditComplete_Click(object sender, EventArgs e)
        {
            //Z.EditSingleData(CompLeteNameTxt, EditCompleteName, CompanyInfo.Name, CompLeteName.Name, GiveUpCompleteName);
            //DataRow[] TabRow = Z.QuerySql().Tables[0].Select("Complete=111");
            //TabRow[0]["CompleteName"] = CompLeteNameTxt.Text;
        }

        private void EditSex_Click(object sender, EventArgs e)
        {
            //Z.EditSingleData(SexTxt, EditSex, CompanyInfo.Name, Sex.Name, GiveUpSex);
        }

        private void EditAddress_Click(object sender, EventArgs e)
        {
            //Z.EditSingleData(AddressTxt, EditAddress, CompanyInfo.Name, Address.Name, GiveUpAddress);
        }

        private void EditCEOName_Click(object sender, EventArgs e)
        {
            //Z.EditSingleData(CEONameTxt, EditCEOName, CompanyInfo.Name, CEOName.Name, GiveUpCEOName);
        }

        private void EditBank_Click(object sender, EventArgs e)
        {
            //Z.EditSingleData(BankTxt, EditBank, CompanyInfo.Name, Bank.Name, GiveUpBank);
        }

        private void EditBankID_Click(object sender, EventArgs e)
        {
            //Z.EditSingleData(BankIDTxt, EditBankID, CompanyInfo.Name, BankID.Name, GiveUpBankID);
        }

        private void EditSociologyID_Click(object sender, EventArgs e)
        {
            //Z.EditSingleData(SociologyIDTxt, EditSociologyID, CompanyInfo.Name, SociologyID.Name, GiveUpSociologyID);
        }

        private void CompanyInfo_Click(object sender, EventArgs e)
        {

        }

        private void GiveUpComplete_Click(object sender, EventArgs e)
        {
            //Z.GiveUpEditSingle(CompLeteNameTxt, EditCompleteName, GiveUpCompleteName, CompanyInfo, CompLeteName);
            //CompLeteNameTxt.Text = Z.SqlQueryNo(CompanyInfo.Name, CompLeteName.Name).ToString();
        }

        private void GiveUpSex_Click(object sender, EventArgs e)
        {
            //Z.GiveUpEditSingle(SexTxt, EditSex, GiveUpSex, CompanyInfo, Sex);
            //SexTxt.Text = Z.SqlQueryNo(CompanyInfo.Name, Sex.Name).ToString();
        }

        private void GiveUpAddress_Click(object sender, EventArgs e)
        {
            //Z.GiveUpEditSingle(AddressTxt, EditAddress, GiveUpAddress, CompanyInfo, Address);
            //AddressTxt.Text = Z.SqlQueryNo(CompanyInfo.Name, Address.Name).ToString();
        }

        private void GiveUpCEOName_Click(object sender, EventArgs e)
        {
            //Z.GiveUpEditSingle(CEONameTxt, EditCEOName, GiveUpCEOName, CompanyInfo, CEOName);
            //CEONameTxt.Text = Z.SqlQueryNo(CompanyInfo.Name, CEOName.Name).ToString();
        }

        private void GiveUpBank_Click(object sender, EventArgs e)
        {
            //Z.GiveUpEditSingle(BankTxt, EditBank, GiveUpBank, CompanyInfo, Bank);
            //BankTxt.Text = Z.SqlQueryNo(CompanyInfo.Name, Bank.Name).ToString();
        }

        private void GiveUpBankID_Click(object sender, EventArgs e)
        {
            //Z.GiveUpEditSingle(BankIDTxt, EditBankID, GiveUpBankID, CompanyInfo, BankID);
            //BankIDTxt.Text = Z.SqlQueryNo(CompanyInfo.Name, BankID.Name).ToString();
        }

        private void GiveUpSociologyID_Click(object sender, EventArgs e)
        {
            //    Z.GiveUpEditSingle(SociologyIDTxt, EditSociologyID, GiveUpSociologyID, CompanyInfo, SociologyID);
            //    SociologyIDTxt.Text = Z.SqlQueryNo(CompanyInfo.Name, SociologyID.Name).ToString();
        }

        private void SaveAll_Click(object sender, EventArgs e)
        {
            //Z.QuerySql() = new DataSet();
            DataRow[] TabRow = Z.QuerySql().Tables["CompanyInfo"].Select("CompLeteName = 'rrr'");
            TabRow[0][1] = CompLeteName.Text;
            TabRow[0][Sex.Name] = Sex.Text;
            TabRow[0][Address.Name] = Address.Text;
            TabRow[0][CEOName.Name] = CEOName.Text;
            TabRow[0][Bank.Name] = Bank.Text;
            TabRow[0][BankID.Name] = BankID.Text;
            TabRow[0][SociologyID.Name] = SociologyID.Text;
            Z.UpDateSql(Z.QuerySql().Tables[0]);
        }

        private void EnterLoad_Click(object sender, EventArgs e)
        {
            //Bitmap bmp = new Bitmap(ImageFile.Image);
            //MemoryStream memStream = new MemoryStream();
            //bmp.Save(memStream, ImageFile.Image.RawFormat);
            //memStream.Seek(0, SeekOrigin.Begin); 
            //byte[] btImage = new byte[memStream.Length];
            //memStream.Read(btImage, 0, btImage.Length);
            //Z.SqlSerCon.Open();
            //SqlCommand SqlCom = new SqlCommand("insert into CompanyImage (ImageName,ImageFile) values ('" + ImageName.Text + "', @ImageFile )", Z.SqlSerCon);
            //SqlCom.Parameters.Add(ImageFile.Name, SqlDbType.Image, btImage.Length).Value = btImage;
            //SqlCom.ExecuteNonQuery();
            //Z.SqlSerCon.Close();
            //memStream.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            using (JokerEntities en = new JokerEntities())
            {
                Personnel p = en.Personnels.FirstOrDefault(x => x.PSex == "女");
                if (p == null)
                {
                    MessageBox.Show("不存在女员工！");
                    return;
                }

                //修改第一个女员工的PStaffID为guid
                p.PStaffID = Guid.NewGuid().ToString();
                en.SaveChanges();
                MessageBox.Show("修改成功！");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //删除最后一个员工
            using (JokerEntities en = new JokerEntities())
            {
                Personnel p = en.Personnels.ToList().Last();
                en.Personnels.Remove(p);
                en.SaveChanges();
                MessageBox.Show("删除成功！");
            }
        }

        private void btnGetImage_Click(object sender, EventArgs e)
        {
            using (JokerEntities en = new JokerEntities())
            {
                CompanyImage img = en.CompanyImages.FirstOrDefault();
                if (img == null)
                {
                    MessageBox.Show("没找到图片！");
                }
                else
                {
                    try
                    {
                        this.ImageFile.Image = null;
                        this.ImageFile.Image = Image.FromStream(new MemoryStream(img.ImageFile));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
    }
}
